package com.example.teststatisticesinterface.activity;

import com.example.teststatisticesinterface.R;
import com.example.teststatisticesinterface.util.Contstant;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ProgressBar;
import android.widget.TextView;

public class DbTableActivity extends Activity {
	private GridView gridView;
	private HorizontalScrollView horizontalScrollView;
	
	private LinearLayout mLayout,dbtable_layout;
	private Button btn_checked,btn_addView,btn_removeView;
	private TextView tv_checked;
	
	private boolean flag=true;
	
	private TextView mTextView;
	// 宽度
	private int cWidth = 240;
	// 水平间距
	private int hSpacing = 10;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dbtable_activity);
//		setProgressBarVisibility(true);
		initView();
	}

	private void initView() {
		
		
		btn_addView=(Button) findViewById(R.id.btn_addview);
		btn_addView.setOnClickListener(myClickListener);
		btn_removeView=(Button) findViewById(R.id.btn_removeview);
		btn_removeView.setOnClickListener(myClickListener);
		
		gridView = (GridView) findViewById(R.id.mGridView);
		horizontalScrollView = (HorizontalScrollView) findViewById(R.id.mScrollView);
		horizontalScrollView.setHorizontalScrollBarEnabled(false);
		
		mLayout=(LinearLayout) findViewById(R.id.llayou_head);
		btn_checked=(Button) findViewById(R.id.btn_allchecked);
		tv_checked=(TextView) findViewById(R.id.tv_checked);
		
		dbtable_layout=(LinearLayout)findViewById(R.id.layout_dbtable);
		
		mTextView=(TextView) findViewById(R.id.tv_long);
		mTextView.setText(Contstant.TextValue);
		
		mTextView.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				ViewGroup.LayoutParams params= mTextView.getLayoutParams();
				if (flag) {
					params.height=200;
					mTextView.setLayoutParams(params);
					mTextView.invalidate();
					flag=false;
				}else {
					params.height=LayoutParams.WRAP_CONTENT;
					mTextView.setLayoutParams(params);
					mTextView.invalidate();
					flag=true;
				}
			}
		});
		
	
		MyGridAdapter myGridAdapter = new MyGridAdapter(DbTableActivity.this);
		gridView.setAdapter(myGridAdapter);

		LayoutParams params = new LayoutParams(myGridAdapter.getCount() * (48 + 10), LayoutParams.WRAP_CONTENT);
		
		gridView.setLayoutParams(params);
		gridView.setColumnWidth(cWidth);
		gridView.setHorizontalSpacing(hSpacing);
		gridView.setStretchMode(GridView.NO_STRETCH);
		gridView.setNumColumns(myGridAdapter.getCount());
		gridView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				ProgressBar progressBar=new ProgressBar(DbTableActivity.this);
				progressBar.setMax(100);
//				progressBar.set
				
			}
		});
		

	}
	 
	private OnClickListener myClickListener=new OnClickListener() {
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.btn_addview:
				LinearLayout layout=new LinearLayout(DbTableActivity.this);
				LinearLayout.LayoutParams params=new LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
				layout.setLayoutParams(params);
				
				TextView content=new TextView(DbTableActivity.this);
				content.setText("text");
				
				layout.addView(content);
				dbtable_layout.addView(layout);
				
				break;
			case R.id.btn_removeview:
				
				
				
				break;

			default:
				break;
			}
		}
	};

	class MyGridAdapter extends BaseAdapter {
		Context context;
		LayoutInflater mInflater;

		/**
		 * @param context
		 * @param mInflater
		 */
		public MyGridAdapter(Context context) {
			super();
			this.context = context;
			mInflater = LayoutInflater.from(context);
		}

		@Override
		public int getCount() {
			return 60;
		}

		@Override
		public Object getItem(int position) {
			return position;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder viewHolder;
			if (convertView == null) {
				viewHolder = new ViewHolder();
				convertView = mInflater.inflate(R.layout.gridview_item, null);
				viewHolder.mImageView = (ImageView) findViewById(R.id.mImage);
				viewHolder.mtTextView = (TextView) findViewById(R.id.mText);
			} else {
				viewHolder = (ViewHolder) convertView.getTag();
			}
			convertView.setTag(viewHolder);

			return convertView;
		}

	}

	static class ViewHolder {
		ImageView mImageView;
		TextView mtTextView;
		

//要动态设置焦点，就用view.requestFocus()函数实现。。
	}

}
